{% extends "base/admin.html" %}

{% block title %}文章管理{% endblock %}

{% block head %}
    <!-- Data Tables -->
    <link href="/static/admin/css/plugins/dataTables/dataTables.bootstrap.css" rel="stylesheet">
{% endblock %}

{% block content %}
        {% set data = g.api.blog_get_all().data %}
        <div class="row">
            <div class="col-sm-12">
                <div class="ibox float-e-margins">
                    <div class="ibox-title">
                        <h5>文章管理</h5><small><a href="{{ url_for('front.blogWrite') }}" target="_blank">&nbsp;创作</a></small>
                    </div>
                    <div class="ibox-content">
                        <table class="table table-striped table-bordered table-hover dataTables-example">
                            <thead>
                                <tr>
                                    <th>ID</th>
                                    <th>标题</th>
                                    <th>创作</th>
                                    <th>更新</th>
                                    <th>标签</th>
                                    <th>分类</th>
                                    <th>作者</th>
                                    <th>推荐</th>
                                    <th>置顶</th>
                                    <th>动作</th>
                                </tr>
                            </thead>
                            <tbody>
                                {% for blog in data %}
                                <tr>
                                    <td>{{ blog.id }}</td>
                                    <td>{{ blog.title }}</td>
                                    <td>{{ blog.create_time }}</td>
                                    <td>{{ blog.update_time }}</td>
                                    <td>{{ blog.tag }}</td>
                                    <td>{{ blog.sources }}: {{ blog.catalog }}</td>
                                    <td>{{ blog.author }}</td>
                                    <td>{% if blog.top in ('true', 'True', True) %}已置顶{% else %}未置顶{% endif %}</td>
                                    <td>{% if blog.recommend in ('true', 'True', True) %}已推荐{% else %}未推荐{% endif %}</td>
                                    <td class="center">
                                        <div class="btn-group">
                                            <button data-toggle="dropdown" class="btn btn-success dropdown-toggle btn-rounded btn-outline" aria-expanded="false">{{ blog.id }}<span class="caret"></span></button>
                                            <ul class="dropdown-menu" role="menu">
                                                <li><a href="{{ url_for('front.blogShow', bid=blog.id) }}" target="_blank"><scan class="text-muted">查看此文章</scan></a></li>
                                                <li><a href="{{ url_for('front.blogEdit', blogId=blog.id) }}" target="_blank"><scan class="text-warning">编辑此文章</scan></a></li>
                                            {% if blog.top in ('true', 'True', True) %}
                                                <li><a href="javascript:setTop({{ blog.id}}, false)"><scan class="text-primary">取消置顶此文章</scan></a></li>
                                            {% else %}
                                                <li><a href="javascript:setTop({{ blog.id}}, true)"><scan class="text-primary">置顶此文章</scan></a></li>
                                            {% endif %}
                                            {% if blog.recommend in ('true', 'True', True) %}
                                                <li><a href="javascript:setRecommend({{ blog.id}}, false)"><scan class="text-info">取消推荐此文章</scan></a></li>
                                            {% else %}
                                                <li><a href="javascript:setRecommend({{ blog.id}}, true)"><scan class="text-info">推荐此文章</scan></a></li>
                                            {% endif %}
                                                <li><a href="javascript:delBlog({{ blog.id}})"><scan class="text-danger">删除此文章</scan></a></li>
                                            </ul>
                                        </div>
                                    </td>
                                </tr>
                                {% endfor %}
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>
{% endblock %}

{% block script %}
    <!-- Data Tables -->
    <script src="/static/admin/js/plugins/dataTables/jquery.dataTables.js"></script>
    <script src="/static/admin/js/plugins/dataTables/dataTables.bootstrap.js"></script>
    <!-- Page-Level Scripts -->
    <script>
        $(document).ready(function () {
            $('.dataTables-example').dataTable();

            /* Init DataTables */
            var oTable = $('#editable').dataTable();
        });

// 设置或取消置顶文章
function setTop(blogId,value) {
    $.ajax({
        url: "{{ url_for('api.misc') }}?action=top&blogId="+blogId+"&value=" + value,
        method: 'POST',
        datatype: 'json',
        success: function(res) {
            console.log(res);
            if (res.success==true) {
                if(value==false) {
                    layer.msg("取消置顶成功！");
                } else {
                    layer.msg("置顶文章成功！");
                }
            } else {
                if(value==false) {
                    layer.msg("取消置顶失败！"+res.msg, {icon: 2, time: 15*1000});
                } else {
                    layer.msg("置顶文章失败！"+res.msg, {icon: 2, time: 15*1000});
                }
            }
            document.location.reload()
        },
        error: function(error){
            console.log(error);
        }
    });
}

// 设置或取消推荐文章
function setRecommend(blogId,value) {
    $.ajax({
        url: "{{ url_for('api.misc') }}?action=recommend&blogId="+blogId+"&value=" + value,
        method: 'POST',
        datatype: 'json',
        success: function(res) {
            console.log(res);
            if (res.success==true) {
                if(value==false) {
                    layer.msg("取消推荐成功！");
                } else {
                    layer.msg("推荐文章成功！");
                }
            } else {
                if(value==false) {
                    layer.msg("取消推荐失败！"+res.msg, {icon: 2, time: 15*1000});
                } else {
                    layer.msg("推荐文章失败！"+res.msg, {icon: 2, time: 15*1000});
                }
            }
            document.location.reload()
        },
        error: function(error){
            console.log(error);
        }
    });
}

// 删除文章
function delBlog(blogId) {
    layer.confirm('你确定要删除ID为 '+blogId+' 的文章?', {
        btn: ['确认删除','我再想想']
    }, function(){
        $.ajax({
            url: "{{ url_for('api.blog') }}?blogId="+blogId,
            method: 'DELETE',
            async: false,
            datatype: 'json',
            success: function(res) {
                console.log(res);
                if (res.success==true) {
                    layer.msg("删除文章成功！", {icon: 1, time: 3*1000});
                    document.location.reload()
                } else {
                    layer.msg("删除文章失败！"+res.msg, {icon: 2, time: 15*1000});
                }
            },
            error: function(error){
                console.log(error);
                layer.alert(res.statusText);
            }
        });
    }, function() {
        console.log("canel delete blog");
    });
}
    </script>

{% endblock %}